import { computed, reactive, type Ref, ref } from 'vue'
import { Mode } from '@/core/interfaces/common'
import { MatchTypeEnum, ScopeTypeEnum } from '@/core/enums/negative'
import getNegativeWords from '@/services/promotion/getNegativeWords'
import updateNegativeWords, { type NegativeWordParams } from '@/services/promotion/updateNegativeWords'
import { message } from 'ant-design-vue'
import type { NegativeWord } from '@/core/interfaces/negative'
import type { PromotionDetail } from '@/services/promotion/getPromotionDetail'
import { cloneDeep, isNil } from 'lodash-es'
import removeNegativeWords from '@/services/promotion/removeNegativeWords'
import type { Creative } from '@/core/interfaces/promotion'

export function useNegativeWords(detailInfo: Ref<Partial<PromotionDetail>>) {
  const mode = ref(Mode.create)
  const visible = ref(false)
  const defaultFormFields = () => ({
    name: '',
    matchType: undefined,
    scopeType: undefined,
    keyword: undefined,
    groupId: undefined,
    orderWordId: undefined,
    campaignId: undefined,
    orderId: undefined
  })
  const defaultQueryFields = () => ({
    matchType: MatchTypeEnum.All,
    name: '',
    orderWordId: undefined
  })
  const defaultFormData = () => ({
    keywordDisable: false,
    scopeTypeDisable: false
  })

  const formFields = ref<Partial<NegativeWordParams>>(defaultFormFields())
  const formData = ref(defaultFormData())
  const queryFields = reactive(defaultQueryFields())
  const pagination = reactive({
    current: 1,
    pageSize: 15,
    total: 0
  })
  const orderId = computed(() => {
    return detailInfo.value.orderId
  })

  const [negativeWords, fetchNegativeWords, negativeLoading] = getNegativeWords()
  const [, submitNegativeWords, submitting] = updateNegativeWords()
  const [, deleteNegativeWords] = removeNegativeWords()

  async function loadNegativeWords() {
    if (isNil(orderId.value)) return

    const [err, res] = await fetchNegativeWords({
      orderId: orderId.value,
      matchType: queryFields.matchType,
      name: queryFields.name,
      scopeType: ScopeTypeEnum.All,
      orderWordId: queryFields.orderWordId || 0,
      pageSize: pagination.pageSize,
      pageNum: pagination.current
    })

    if (!err) {
      pagination.total = res.data.data.totalSize
    }
  }

  function handleNegativeAdd() {
    visible.value = true
    mode.value = Mode.create
    formFields.value = defaultFormFields()
    formData.value = defaultFormData()
    formFields.value.orderId = orderId.value
  }

  function handleNegativeAddWithWord(record: Creative) {
    handleNegativeAdd()
    formFields.value.scopeType = ScopeTypeEnum.Unit
    formFields.value.groupId = record.groupId
    formFields.value.campaignId = record.campaignId
    formFields.value.keyword = record.word
    formFields.value.orderWordId = record.orderWordId
    formData.value.keywordDisable = true
    formData.value.scopeTypeDisable = true
  }

  function handleNegativeEdit(record: NegativeWord) {
    visible.value = true
    mode.value = Mode.edit
    formFields.value = defaultFormFields()
    formData.value = defaultFormData()
    formFields.value.orderId = orderId.value
    formFields.value = cloneDeep(record)
  }

  async function handleNegativeDelete(record: NegativeWord) {
    const [err, res] = await deleteNegativeWords({
      negativeWordId: record.id
    })

    if (!err) {
      message.success(res.data.msg)
      await loadNegativeWords()
    }
  }

  function handleNegativeReset() {
    Object.assign(queryFields, defaultQueryFields())
  }

  return {
    mode,
    visible,
    formFields,
    queryFields,
    formData,
    pagination,
    negativeWords,
    negativeLoading,
    submitting,
    loadNegativeWords,
    submitNegativeWords,
    handleNegativeAdd,
    handleNegativeEdit,
    handleNegativeDelete,
    handleNegativeReset,
    handleNegativeAddWithWord
  }
}
